﻿using System;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using UniMana.Data;

namespace UniMana.Win.Training
{
    public partial class FrmStudent_Class_Edit_StudentID : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        Training_StudentInfo studentInfo;
        Training_Student_ClassInfo studentClassInfo;
        Training_ClassInfo classInfo;
        string m_strClassID = "";
        string m_strTraining_StudentKey = "";
        int m_iTraining_ClassKey = 0;

        public FrmStudent_Class_Edit_StudentID(string m_strTraining_StudentKey, int m_iTraining_ClassKey)
        {
            InitializeComponent();
            try
            {
                studentInfo = new Training_StudentInfo(m_strTraining_StudentKey);
                classInfo = new Training_ClassInfo(m_iTraining_ClassKey);
                studentClassInfo = new Training_Student_ClassInfo(m_strTraining_StudentKey, m_iTraining_ClassKey, "nguyen.tuanan");
                this.m_strTraining_StudentKey = m_strTraining_StudentKey;
                this.m_iTraining_ClassKey = m_iTraining_ClassKey;
            }
            catch { }
        }

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void FrmStudent_Class_Edit_StudentID_Load(object sender, EventArgs e)
        {
            try
            {
                txtName.Text = studentInfo.Properties.LastName + " " + studentInfo.Properties.SureName;
                txtBirthday.Text = studentInfo.Properties.Birthday.ToString();
                if (Convert.ToBoolean(studentInfo.Properties.Gender))
                    txtGender.Text = "Nữ";
                else
                    txtGender.Text = "Nam";
            }
            catch { }
            try
            {
                txtClass.Text = classInfo.Properties.Name.ToString();
                txtIDCurrent.Text = studentClassInfo.Properties.ID.ToString();
            }
            catch { }
        }

        /// <summary>
        /// Kiểm tra trùng mã sinh viên
        /// </summary>
        /// <param name="_ID"></param>
        /// <returns></returns>

        private bool CheckUpdateID(string _ID)
        { 
            DataTable m_tbl = new DataTable();
            m_tbl = m_data.m_DCReadOnly.Training_Student_Classes.GetByStudentID(_ID).ToDataTable();
            if (m_tbl.Rows.Count != 0)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Chấp nhận thay đổi
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnOK_Click(object sender, EventArgs e)
        {
            if ((txtIDChange.Text != "") && (txtIDCurrent.Text != "") && (txtIDChange.Text != txtIDCurrent.Text))
            {
                if (CheckUpdateID(txtIDChange.Text))
                {
                    DialogResult result = XtraMessageBox.Show("Bạn muốn thay đổi mã sinh viên " + txtName.Text, "Thông báo", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        // thay đổi mã sinh viên
                        studentClassInfo = new Training_Student_ClassInfo(m_strTraining_StudentKey, m_iTraining_ClassKey, "nguyen.tuanan");
                        studentClassInfo.Properties.ID = txtIDChange.Text;
                        studentClassInfo.Save();

                        // lưu user thay đổi mã sinh viên
                        Log_StudentIDEditInfo editInfo = new Log_StudentIDEditInfo();
                        editInfo.Properties.Training_StudentKey = Guid.Parse(m_strTraining_StudentKey);
                        editInfo.Properties.BeforeID = txtIDCurrent.Text;
                        editInfo.Properties.AfterID = txtIDChange.Text;
                        editInfo.Properties.EditUser = Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                        editInfo.Properties.EditDateTime = SysDateTime.Now;
                        editInfo.Save();
                        //XtraMessageBox.Show("Đã thay đổi mã sinh viên");
                        this.Close();
                    }
                }
                else
                    XtraMessageBox.Show("Mã sinh viên " + txtIDChange.Text + " đã tồn tại. Không thể thay đổi");
            }
        }

        /// <summary>
        /// Thoát
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult result = XtraMessageBox.Show("Bạn muốn thoát.", "Thông báo", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
                this.Close();
        }
    }
}
